package com.wink.admin.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;


/**
 * 线程池配置
 * 注意:
 *
 * @author wink
 * @ConfigurationProperties(prefix = "thread-pool") 规范默认使用是-分隔，非-分隔则报错，可以修改成驼峰
 * @date 2020/11/16 14:57:32
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "thread-pool")
public class ThreadPoolConfig {

    /**
     * 线程池设置
     */
    private int corePoolSize;
    private int maximumPoolSize;
    private int keepAliveTime;
    private int workQueueSize;

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(maximumPoolSize);
        executor.setCorePoolSize(corePoolSize);
        executor.setQueueCapacity(workQueueSize);
        executor.setKeepAliveSeconds(keepAliveTime);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

}
